import 'package:badges/badges.dart' as badges;
import 'package:flutter/material.dart';
import 'package:provider/provider.dart';

import '../../models/bo/UnReadMessageCounter.dart';
import 'message_conversation.dart';

class MessageConversationCell extends StatefulWidget {
  final int cid;
  final int senderId;
  final int partnerId;
  final String? convUrl;
  final String? convName;
  final String? lastMsg;
  final String? lastOnlineTime;
  int unreadCnt;

   MessageConversationCell(
      {required this.cid,
      this.lastOnlineTime,
      required this.partnerId,
      required this.senderId,
      required this.convName,
      this.lastMsg,
      this.unreadCnt = 0,
      required this.convUrl});

  @override
  State<MessageConversationCell> createState() =>
      _MessageConversationCellState();
}

class _MessageConversationCellState extends State<MessageConversationCell> {
  @override
  Widget build(BuildContext context) {
    return GestureDetector(
      onTap: () {
        Provider.of<UnReadMessageCounter>(context,listen: false).subUnRead(widget.cid,widget.unreadCnt);
        Navigator.of(context).push(MaterialPageRoute(
            builder: (_) => ConversationDetailPage(widget.convName!, widget.cid,
                widget.senderId, widget.partnerId)));
      },
      child: Container(
        color: Colors.white,
        height: 70,
        child: Row(
          children: [
            Container(
              // color: Colors.blue,
              child: badges.Badge(
                position: badges.BadgePosition.topEnd(top: -14, end: -5),
                showBadge: widget.unreadCnt > 0,
                badgeAnimation:
                    const badges.BadgeAnimation.fade(toAnimate: false),
                badgeContent: widget.unreadCnt > 0
                    ? Text(
                        widget.unreadCnt.toString(),
                        style: TextStyle(color: Colors.white),
                      )
                    : null,
                child: CircleAvatar(
                  radius: 20,
                  backgroundImage: NetworkImage(widget.convUrl!),
                ),
              ),
            ),
            const SizedBox(
              width: 20,
            ),
            Expanded(
              // color: Colors.blue,
              // margin: EdgeInsets.only(top: 10,bottom: 10),
              child: Column(
                crossAxisAlignment: CrossAxisAlignment.start,
                mainAxisAlignment: MainAxisAlignment.spaceEvenly,
                children: [
                  Row(
                    mainAxisAlignment: MainAxisAlignment.spaceBetween,
                    children: [
                      Text(
                        widget.convName!,
                        style: TextStyle(
                          fontWeight: FontWeight.bold,
                          fontSize: 16,
                        ),
                      ),
                      widget.lastOnlineTime == null
                          ? Text("")
                          : Text(widget.lastOnlineTime!),
                    ],
                  ),
                  Text(widget.lastMsg == null ? "" : widget.lastMsg!),
                ],
              ),
            )
          ],
        ),
      ),
    );
  }
}
